home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk4 / patch / inp.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  8KB  |  340 lines

  1. /* $Header: inp.c,v 2.0 86/09/17 15:37:02 lwall Exp $
  2.  *
  3.  * $Log:    inp.c,v $
  4.  * Revision 2.0  86/09/17  15:37:02  lwall
  5.  * Baseline for netwide release.
  6.  * 
  7.  */
  8.  
  9. #include "EXTERN.h"
  10. #include "common.h"
  11. #include "util.h"
  12. #include "pch.h"
  13. #include "INTERN.h"
  14. #include "inp.h"
  15.  
  16. /* Input-file-with-indexable-lines abstract type */
  17.  
  18. static long i_size;            /* size of the input file */
  19. static char *i_womp;            /* plan a buffer for entire file */
  20. static char **i_ptr;            /* pointers to lines in i_womp */
  21.  
  22. static int tifd = -1;            /* plan b virtual string array */
  23. static char *tibuf[2];            /* plan b buffers */
  24. static LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  25. static LINENUM lines_per_buf;        /* how many lines per buffer */
  26. static int tireclen;            /* length of records in tmp file */
  27.  
  28. /* New patch--prepare to edit another file. */
  29.  
  30. void
  31. re_input()
  32. {
  33.     if (using_plan_a) {
  34.     i_size = 0;
  35. #ifndef lint
  36.     if (i_ptr != Null(char**))
  37.         free((char *)i_ptr);
  38. #endif
  39.     if (i_womp != Nullch)
  40.         free(i_womp);
  41.     i_womp = Nullch;
  42.     i_ptr = Null(char **);
  43.     }
  44.     else {
  45.     using_plan_a = TRUE;        /* maybe the next one is smaller */
  46.     Close(tifd);
  47.     tifd = -1;
  48.     free(tibuf[0]);
  49.     free(tibuf[1]);
  50.     tibuf[0] = tibuf[1] = Nullch;
  51.     tiline[0] = tiline[1] = -1;
  52.     tireclen = 0;
  53.     }
  54. }
  55.  
  56. /* Constuct the line index, somehow or other. */
  57.  
  58. void
  59. scan_input(filename)
  60. char *filename;
  61. {
  62.     if (!plan_a(filename))
  63.     plan_b(filename);
  64.     if (verbose) {
  65.     say3("Patching file %s using Plan %s...\n", filename,
  66.       (using_plan_a ? "A" : "B") );
  67.     }
  68. }
  69.  
  70. /* Try keeping everything in memory. */
  71.  
  72. bool
  73. plan_a(filename)
  74. char *filename;
  75. {
  76.     int ifd;
  77.     Reg1 char *s;
  78.     Reg2 LINENUM iline;
  79.  
  80. #ifdef AMIGA
  81.     if (ok_to_create_file && dfindOne(&filestat, filename, 1)) {
  82. #else
  83.     if (ok_to_create_file && stat(filename, &filestat) < 0) {
  84. #endif
  85.     if (verbose)
  86.         say2("(Creating file %s...)\n",filename);
  87.     makedirs(filename, TRUE);
  88.     close(creat(filename, 0666));
  89.     }
  90. #ifdef AMIGA
  91.     if (dfindOne(&filestat, filename, 0))
  92.     fatal2("Can't find %s.\n", filename);
  93. #else
  94.     if (stat(filename, &filestat) < 0) {
  95.     Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
  96.     if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
  97.         Sprintf(buf, CHECKOUT, filename);
  98.         if (verbose)
  99.         say2("Can't find %s--attempting to check it out from RCS.\n",
  100.             filename);
  101.         if (system(buf) || stat(filename, &filestat))
  102.         fatal2("Can't check out %s.\n", filename);
  103.     }
  104.     else {
  105.         Sprintf(buf, "SCCS/%s%s", SCCSPREFIX, filename);
  106.         if (stat(buf, &filestat) >= 0 || stat(buf+5, &filestat) >= 0) {
  107.         Sprintf(buf, GET, filename);
  108.         if (verbose)
  109.             say2("Can't find %s--attempting to get it from SCCS.\n",
  110.             filename);
  111.         if (system(buf) || stat(filename, &filestat))
  112.             fatal2("Can't get %s.\n", filename);
  113.         }
  114.         else
  115.         fatal2("Can't find %s.\n", filename);
  116.     }
  117.     }
  118. #endif
  119. #ifdef AMIGA
  120.     filemode = filestat.fib_Protection ^ 15;
  121.     i_size = filestat.fib_Size;
  122. #else
  123.     filemode = filestat.st_mode;
  124.     if ((filemode & S_IFMT) & ~S_IFREG)
  125.     fatal2("%s is not a normal file--can't patch.\n", filename);
  126.     i_size = filestat.st_size;
  127. #endif
  128.     if (out_of_mem) {
  129.     set_hunkmax();        /* make sure dynamic arrays are allocated */
  130.     out_of_mem = FALSE;
  131.     return FALSE;            /* force plan b because plan a bombed */
  132.     }
  133. #ifdef lint
  134.     i_womp = Nullch;
  135. #else
  136.     i_womp = malloc((MEM)(i_size+2));    /* lint says this may alloc less than */
  137.                     /* i_size, but that's okay, I think. */
  138. #endif
  139.     if (i_womp == Nullch)
  140.     return FALSE;
  141.     if ((ifd = open(filename, 0)) < 0)
  142.     fatal2("Can't open file %s\n", filename);
  143. #ifndef lint
  144.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  145.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  146.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  147.     return FALSE;    /*   undersized. */
  148.     }
  149. #endif
  150.     Close(ifd);
  151.     if (i_size && i_womp[i_size-1] != '\n')
  152.     i_womp[i_size++] = '\n';
  153.     i_womp[i_size] = '\0';
  154.  
  155.     /* count the lines in the buffer so we know how many pointers we need */
  156.  
  157.     iline = 0;
  158.     for (s=i_womp; *s; s++) {
  159.     if (*s == '\n')
  160.         iline++;
  161.     }
  162. #ifdef lint
  163.     i_ptr = Null(char**);
  164. #else
  165.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  166. #endif
  167.     if (i_ptr == Null(char **)) {    /* shucks, it was a near thing */
  168.     free((char *)i_womp);
  169.     return FALSE;
  170.     }
  171.     
  172.     /* now scan the buffer and build pointer array */
  173.  
  174.     iline = 1;
  175.     i_ptr[iline] = i_womp;
  176.     for (s=i_womp; *s; s++) {
  177.     if (*s == '\n')
  178.         i_ptr[++iline] = s+1;    /* these are NOT null terminated */
  179.     }
  180.     input_lines = iline - 1;
  181.  
  182.     /* now check for revision, if any */
  183.  
  184.     if (revision != Nullch) { 
  185.     if (!rev_in_string(i_womp)) {
  186.         if (force) {
  187.         if (verbose)
  188.             say2(
  189. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  190.             revision);
  191.         }
  192.         else {
  193. #ifdef AMIGA
  194.         ask2(
  195. "This file doesn't appear to be the %s version--patch anyway?",
  196.             revision);
  197. #else
  198.         ask2("\
  199. This file doesn't appear to be the %s version--patch anyway? [n] ",
  200.             revision);
  201. #endif
  202.         if (*buf != 'y')
  203.         fatal1("Aborted.\n");
  204.         }
  205.     }
  206.     else if (verbose)
  207.         say2("Good.  This file appears to be the %s version.\n",
  208.         revision);
  209.     }
  210.     return TRUE;            /* plan a will work */
  211. }
  212.  
  213. /* Keep (virtually) nothing in memory. */
  214.  
  215. void
  216. plan_b(filename)
  217. char *filename;
  218. {
  219.     Reg3 FILE *ifp;
  220.     Reg1 int i = 0;
  221.     Reg2 int maxlen = 1;
  222.     Reg4 bool found_revision = (revision == Nullch);
  223.  
  224.     using_plan_a = FALSE;
  225.     if ((ifp = fopen(filename, "r")) == Nullfp)
  226.     fatal2("Can't open file %s\n", filename);
  227.     if ((tifd = creat(TMPINNAME, 0666)) < 0)
  228.     fatal2("Can't open file %s\n", TMPINNAME);
  229.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  230.     if (revision != Nullch && !found_revision && rev_in_string(buf))
  231.         found_revision = TRUE;
  232.     if ((i = strlen(buf)) > maxlen)
  233.         maxlen = i;            /* find longest line */
  234.     }
  235.     if (revision != Nullch) {
  236.     if (!found_revision) {
  237.         if (force) {
  238.         if (verbose)
  239.             say2(
  240. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  241.             revision);
  242.         }
  243.         else {
  244. #ifdef AMIGA
  245.         ask2(
  246. "This file doesn't appear to be the %s version--patch anyway?",
  247.             revision);
  248. #else
  249.         ask2("\
  250. This file doesn't appear to be the %s version--patch anyway? [n] ",
  251.             revision);
  252. #endif
  253.         if (*buf != 'y')
  254.             fatal1("Aborted.\n");
  255.         }
  256.     }
  257.     else if (verbose)
  258.         say2("Good.  This file appears to be the %s version.\n",
  259.         revision);
  260.     }
  261.     Fseek(ifp, 0L, 0);        /* rewind file */
  262.     lines_per_buf = BUFFERSIZE / maxlen;
  263.     tireclen = maxlen;
  264.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  265.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  266.     if (tibuf[1] == Nullch)
  267.     fatal1("Can't seem to get enough memory.\n");
  268.     for (i=1; ; i++) {
  269.     if (! (i % lines_per_buf))    /* new block */
  270.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  271.         fatal1("patch: can't write temp file.\n");
  272.     if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  273.       == Nullch) {
  274.         input_lines = i - 1;
  275.         if (i % lines_per_buf)
  276.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  277.             fatal1("patch: can't write temp file.\n");
  278.         break;
  279.     }
  280.     }
  281.     Fclose(ifp);
  282.     Close(tifd);
  283.     if ((tifd = open(TMPINNAME, 0)) < 0) {
  284.     fatal2("Can't reopen file %s\n", TMPINNAME);
  285.     }
  286. }
  287.  
  288. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  289.  
  290. char *
  291. ifetch(line,whichbuf)
  292. Reg1 LINENUM line;
  293. int whichbuf;                /* ignored when file in memory */
  294. {
  295.     if (line < 1 || line > input_lines)
  296.     return "";
  297.     if (using_plan_a)
  298.     return i_ptr[line];
  299.     else {
  300.     LINENUM offline = line % lines_per_buf;
  301.     LINENUM baseline = line - offline;
  302.  
  303.     if (tiline[0] == baseline)
  304.         whichbuf = 0;
  305.     else if (tiline[1] == baseline)
  306.         whichbuf = 1;
  307.     else {
  308.         tiline[whichbuf] = baseline;
  309. #ifndef lint        /* complains of long accuracy */
  310.         Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  311. #endif
  312.         if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  313.         fatal2("Error reading tmp file %s.\n", TMPINNAME);
  314.     }
  315.     return tibuf[whichbuf] + (tireclen*offline);
  316.     }
  317. }
  318.  
  319. /* True if the string argument contains the revision number we want. */
  320.  
  321. bool
  322. rev_in_string(string)
  323. char *string;
  324. {
  325.     Reg1 char *s;
  326.     Reg2 int patlen;
  327.  
  328.     if (revision == Nullch)
  329.     return TRUE;
  330.     patlen = strlen(revision);
  331.     for (s = string; *s; s++) {
  332.     if (isspace(*s) && strnEQ(s+1, revision, patlen) && 
  333.         isspace(s[patlen+1] )) {
  334.         return TRUE;
  335.     }
  336.     }
  337.     return FALSE;
  338. }
  339.  
  340.